<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        // 访问对象属性时间复杂度为 O(n)
        // 访问常量或者根据索引访问数组时间复杂度为 O(1)
        function search() {
            console.time("search")
            for (let i = 0; i < 100; i++) {
                const query = window.location.href.substring(window.location.href.indexOf('?'))
            }
            console.timeEnd("search")
        }

        function betterSearch() {
            console.time("better")
            for (let i = 0; i < 100; i++) {
                const url = window.location.href
                const query = url.substring(url.indexOf('?'))
            }
            console.timeEnd("better")
        }

        function bestSearch() {
            console.time("best")
            const url = window.location.href
            for (let i = 0; i < 100; i++) {
                const query = url.substring(url.indexOf('?'))
            }
            console.timeEnd("best")
        }

        search()            // search: 0.10009765625 ms
        betterSearch()      // better: 0.047119140625 ms  
        bestSearch()        // best: 0.010986328125 ms



        

    </script>
</body>

</html>